/**
 * Created by elead on 2019/1/2.
 */
function getTypeCFn(addFlage,type,arrAdd){
    if(addFlage!=undefined){
        var nameArr=arrAdd?arrAdd:"";
        var classType=type?type:"";
    }else{
        var nameArr=getQueryStringByNameTwo("nameArr")?getQueryStringByNameTwo("nameArr"):"";
        nameArr=decodeURIComponent(nameArr);
        var classType=getQueryStringByNameTwo("checkVal")?getQueryStringByNameTwo("checkVal"):"";
    }

    if(classType=="POL"){
        $(".tabRight").css({
            "margin-left":"150px"
        })
    }
    var getLocalStorage=localStorage.getItem("commonClassTypeC")?localStorage.getItem("commonClassTypeC"):"";
    getLocalStorage=getLocalStorage.split(",");
    if(getLocalStorage.length>0){
        //window.localStorage.removeItem("commonClassType")
    }

    createLeftTab();
    function createLeftTab(){
        nameArr=nameArr.replace(/[\ |\[|\]]/g,"");
        nameArr=nameArr.split(",");
        var leftTable="";
        for(var i=0;i<nameArr.length;i++){
            leftTable+='<tr><td><input type="checkbox"/></td><td>'+nameArr[i]+'</td></tr>'
        }
        $(".tabLeft").append(leftTable);
    }
    createRightTab();
    function createRightTab(){
        var publicClass=commonObj.CPublicAttName;
        var publicText=commonObj.CEditAttName;
        var newPubArr=[];
        for(var j=0;j<publicText.length;j++){
            newPubArr.push(publicText[j])
        }
        newPubArr.push.apply(newPubArr,publicClass);
        var Title="<thead><tr><td title='物料编号'><span>物料编号</span></td><td title='名称'><span>名称</span></td>";
        for(var i=0;i<newPubArr.length;i++){
            if(newPubArr[i]!="成品型号"&&newPubArr[i]!="分类属性（C1时，多个分类属性以“;”分割,如：001；002；003）"&&i<publicText.length){
                Title+="<td title='"+newPubArr[i]+"'><span><i style='color:red'>*</i>"+newPubArr[i]+"</span></td>";
            }else{
                Title+="<td title='"+newPubArr[i]+"'><span>"+newPubArr[i]+"</span></td>";
            }

        }
        Title+="</tr></thead>";
        if(addFlage==undefined||addFlage==""){
            $(".tabRight").append(Title);
            var tableTd="<tbody>"
            for(var n=0;n<nameArr.length;n++){
                tableTd+="<tr><td title='<自动生成>'><input readonly='readonly' unselectable='on' type='text' value='<自动生成>'/></td><td title='<自动生成>'><input readonly='readonly' unselectable='on' type='text' value='<自动生成>'/></td>";
                for(var m=0;m<newPubArr.length;m++){
                    if(m==1){
                        tableTd+="<td ><input readonly='readonly' unselectable='on' type='text' value=''/></td>"
                    }else if(m==2){
                        tableTd+="<td title='"+nameArr[n]+"'><input readonly='readonly' unselectable='on' type='text' value='"+nameArr[n]+"'/></td>";
                    }else{
                        var ceLen=publicText.length;
                        if(m>ceLen||m==ceLen){
                            var getLocalStorageText=getLocalStorage[m-ceLen]!=undefined?getLocalStorage[m-ceLen]:"";
                            getLocalStorageText=getLocalStorageText.split("$#")
                            tableTd+="<td class='publicAttributes' style='background:#eee; opacity:0.6;filter:Alpha(opacity=60)' title='"+getLocalStorageText[0]+"' data-submitData='"+getLocalStorageText[1]+"'>"+getLocalStorageText[0]+"</td>"
                            //tableTd+="<td>"+getLocalStorage[m-ceLen]+"</td>"
                        }else{
                            if(publicText[m]=="分类属性（C1时，多个分类属性以“;”分割,如：001；002；003）"){
                                tableTd+="<td class='categoricalAttribute'><input maxlength='50' type='text' value=''/></td>"
                            }else{
                                tableTd+="<td><input maxlength='50' type='text' value/></td>"
                            }


                        }
                    }

                }
                tableTd+="</tr>";
            }
            tableTd+="</tbody>";
        }else{
            var tableTd="";
            for(var n=0;n<nameArr.length;n++){
                //tableTd+="<tr class='addFlage"+arrAdd+"'><td><input readonly='readonly' unselectable='on' type='text'/></td><td><input readonly='readonly' unselectable='on' type='text'/></td>";
                tableTd+="<tr class='addFlage"+arrAdd+"'><td title='<自动生成>'><input readonly='readonly' unselectable='on' type='text' value='<自动生成>'/></td><td title='<自动生成>'><input readonly='readonly' unselectable='on' type='text' value='<自动生成>'/></td>";
                for(var m=0;m<newPubArr.length;m++){
                    if(m==1){
                        tableTd+="<td><input readonly='readonly' unselectable='on' type='text' value=''/></td>"
                    }else if(m==2){
                        tableTd+="<td><input readonly='readonly' unselectable='on' type='text' value='"+nameArr[n]+"'/></td>"
                    }else{
                        var ceLen=publicText.length;
                        if(m>ceLen||m==ceLen){
                            var getLocalStorageText=getLocalStorage[m-ceLen]!=undefined?getLocalStorage[m-ceLen]:"";
                            getLocalStorageText=getLocalStorageText.split("$#")
                            tableTd+="<td class='publicAttributes' style='background:#eee; opacity:0.6;filter:Alpha(opacity=60)' title='"+getLocalStorageText[0]+"' data-submitData='"+getLocalStorageText[1]+"'>"+getLocalStorageText[0]+"</td>"
                        }else{

                            tableTd+="<td><input maxlength='30' type='text' value/></td>"

                        }
                    }

                }
                tableTd+="</tr>";
            }
        }



        $(".tabRight").append(tableTd);
        var height=$(".tabRight thead").height();
        $(".tabLeft thead tr").css("height",height+"px");
        var otdHeight=$(".tabRight tbody tr")/*.height()*/;
        for(var w=0;w<otdHeight.length;w++){
            var offHeight= $(".tabRight tbody tr")[w].clientHeight;
            $(".tabLeft tbody tr").eq(w).css({
                "offsetHeight":otdHeight+"px"
            });
        }
        var listText="";
        function renderForm2(str,m,selected){
            layui.use('form', function(){
                var form = layui.form;//高版本建议把括号去掉，有的低版本，需要加()
                var option="";
                var selt=selected!=undefined&&selected!=""?true:false;
                for(var i=0;i<str.length;i++){
                    if(selt&&selected==str[i]){
                        option+='<option value="" selected>'+str[i]+'</option>'
                    }else{
                        option+='<option value="" >'+str[i]+'</option>'
                    }
                }
                listText='<form class="layui-form" action=""> <div class="layui-form-item proSelect"> <div class="layui-input-block editWidth"> <select lay-ignore class="select'+m+'" name="" lay-verify=""> <option value=""></option>'+option+' </select> </div> </div></form>';
                form.render("select");
            });
        }
        if(addFlage!=undefined&&addFlage!=""){
            var typeName=$(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(5) input").val();
            var otrOrSelect=$(".tabRight tbody .addFlage");
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(4) input").remove();
            var defaultUnit="";
            if(typeName=="C1"||typeName=="C8"||typeName=="C9"||typeName=="L1"||typeName=="L3"||typeName=="S3"){
                defaultUnit="SHT"
            }else if(typeName=="E1"||typeName=="E12"||typeName=="E3"||typeName=="E4"||typeName=="E6"||typeName=="C3"||typeName=="C6"){
                defaultUnit="PCS"
            }else {
                defaultUnit="EAE"
            }
                renderForm2(commonObj.CUnit,"Unitflage"+arrAdd,defaultUnit);
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(4)").append(listText);
            var defaultCutting="";
            if(typeName=="C1"||typeName=="C8"||typeName=="C9"||typeName=="L1"||typeName=="L3"||typeName=="S3"){
                defaultCutting="整张"
            }else if(typeName=="E1"||typeName=="E12"||typeName=="E3"||typeName=="E4"||typeName=="E6"||typeName=="C3"||typeName=="C6"){
                defaultCutting="小粒"
            }else {
                defaultCutting=""
            }
                renderForm2(commonObj.CuttingMode,"flage"+arrAdd,defaultCutting);
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(8) input").remove();
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(8)").append(listText);
                renderForm2(commonObj.ThinningThicknes,"Tflage"+arrAdd);
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(9) input").remove();
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(9)").append(listText);
                if(typeName=="C7"||typeName=="C8"){
                    renderForm2(commonObj.Coatingtype,"Cflage"+arrAdd);
                }else{
                    renderForm2(commonObj.Coatingtype,"Cflage"+arrAdd,"NA");
                }
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(6) input").remove();
                $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(6)").append(listText);
            var selectitem2='<form class="layui-form" action=""> <div class="layui-form-item proSelect"> <div class="layui-input-block editWidth"> <select lay-ignore class="selectPSG'+arrAdd+'" name="" lay-verify=""> <option value=""></option> </select> </div> </div></form>';
            var partSingleGlassThickness=commonObj.partSingleGlassThickness;
            renderForm2(partSingleGlassThickness,"PSG"+arrAdd);
            $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(7) input").remove();
            $(".tabRight tbody .addFlage"+arrAdd).find("td:nth-child(7)").append(selectitem2);

        }else{
            var otrOrSelect=$(".tabRight tbody tr");
            for(var k=0;k<otrOrSelect.length;k++){
                var typeName=$(".tabRight tbody tr").eq(k).find("td:nth-child(5) input").val();
                $(".tabRight tbody tr").eq(k).find("td:nth-child(4) input").remove();
                var unit ='<form class="layui-form" action=""> <div class="layui-form-item proSelect"> <div class="layui-input-block editWidth"> <select lay-ignore class="selectUnit'+k+'" name="" lay-verify=""> <option value=""></option> </select> </div> </div></form>';
                var defaultUnit="";
                if(typeName=="C1"||typeName=="C8"||typeName=="C9"||typeName=="L1"||typeName=="L3"||typeName=="S3"){
                    defaultUnit="SHT"
                }else if(typeName=="E1"||typeName=="E12"||typeName=="E3"||typeName=="E4"||typeName=="E6"||typeName=="C3"||typeName=="C6"){
                    defaultUnit="PCS"
                }else {
                    defaultUnit="EAE"
                }
                renderForm(commonObj.CUnit,"Unit"+k,"","","",defaultUnit);
                $(".tabRight tbody tr").eq(k).find("td:nth-child(4)").append(unit);

                var selectitem='<form class="layui-form" action=""> <div class="layui-form-item proSelect"> <div class="layui-input-block editWidth"> <select lay-ignore class="select'+k+'" name="" lay-verify=""> <option value=""></option> </select> </div> </div></form>';
                var arr=commonObj.CuttingMode;
                var defaultCutting="";
                if(typeName=="C1"||typeName=="C8"||typeName=="C9"||typeName=="L1"||typeName=="L3"||typeName=="S3"){
                    defaultCutting="整张"
                }else if(typeName=="E1"||typeName=="E12"||typeName=="E3"||typeName=="E4"||typeName=="E6"||typeName=="C3"||typeName=="C6"){
                    defaultCutting="小粒"
                }else {
                    defaultCutting=""
                }
                renderForm(arr,k,"",'','',defaultCutting)
                $(".tabRight tbody tr").eq(k).find("td:nth-child(8) input").remove();
                $(".tabRight tbody tr").eq(k).find("td:nth-child(8)").append(selectitem);

                var selectitemThin='<form class="layui-form" action=""> <div class="layui-form-item proSelect"> <div class="layui-input-block editWidth"> <select lay-ignore class="selectT'+k+'" name="" lay-verify=""> <option value=""></option> </select> </div> </div></form>';
                var ThinningThicknes=commonObj.ThinningThicknes
                renderForm(ThinningThicknes,"T"+k)
                $(".tabRight tbody tr").eq(k).find("td:nth-child(9) input").remove();
                $(".tabRight tbody tr").eq(k).find("td:nth-child(9)").append(selectitemThin);
                var selectitem1='<form class="layui-form" action=""> <div class="layui-form-item proSelect"> <div class="layui-input-block editWidth"> <select lay-ignore class="selectC'+k+'" name="" lay-verify=""> <option value=""></option> </select> </div> </div></form>';
                var selectOption=commonObj.Coatingtype;
                if(typeName=="C7"||typeName=="C8"){
                    renderForm(selectOption,"C"+k);
                }else{
                    renderForm(selectOption,"C"+k,"","","","NA");
                }

                $(".tabRight tbody tr").eq(k).find("td:nth-child(6) input").remove();
                $(".tabRight tbody tr").eq(k).find("td:nth-child(6)").append(selectitem1);
                var selectitem2='<form class="layui-form" action=""> <div class="layui-form-item proSelect"> <div class="layui-input-block editWidth"> <select lay-ignore class="selectPSG'+k+'" name="" lay-verify=""> <option value=""></option> </select> </div> </div></form>';
                var partSingleGlassThickness=commonObj.partSingleGlassThickness;
                renderForm(partSingleGlassThickness,"PSG"+k);
                $(".tabRight tbody tr").eq(k).find("td:nth-child(7) input").remove();
                $(".tabRight tbody tr").eq(k).find("td:nth-child(7)").append(selectitem2);
            }
        }
        var inputLen=$("tr td");
        for(var h=0;h<inputLen.length;h++){
            if($("tr td ").eq(h).find("input").length>0){
                if($("tr td ").eq(h).find("input").attr("readonly")=="readonly"){
                    $("tr td").eq(h).css({
                        "background":"#eee",
                        "opacity":0.6,
                        "filter":"Alpha(opacity=60)"
                    })
                }
                var inputVal=$("tr td").eq(h).find("input").val();

            }
            if($("tr td ").eq(h).find("select").length>0){
                var selectText=$("tr td").eq(h).find("select option:selected").text();
                $("tr td").eq(h).attr("title",selectText);
            }
        }


    }

    //提交
    $(".submitMes").click(function(){
        var trLen=$(".tabRight tbody tr");
        var tdLen=$(".tabRight tbody tr:first-child td");
        var dataListObj={
            "number":"",//物料编号
            "name":"",//物料名称
            "synchysis":"",//是否混版及混版尺寸
            "defaultUnit":"",//默认单位
            "SemiFinishedGoodType":'',//半成品分类
            "Coatingtype":"",//镀膜类型
            "partSingleGlassThickness":"",//单玻璃厚度
            "cuttingMode":"",//切割方式
            "processingModeAndThickness":"",//加工方式及薄化前后厚度
            "Integration":"",//集成度（格式如：60PANEL）
            "finishedGoodType":"",//成品型号
            "classificationATT":"",//分类属性
            "createType":"",//类型（成品/半成品）
            "source":"",//源
            "partNumberEffectiveSite":"",//料号产地
            "finishedGoodSize":"",//产品尺寸
            "otherorigin":"",//其他生效地及源类型（如只需在一地生效，请选“无”）
            "finishedGoodLineBody":"",//线体
            "partDrawingNumber":"",//图纸编号/工艺文件编号（申请T1时必填，如：TM033HS01)）
            "partInspectionStandard":"",//检验标准:
            "partSupplier":"",//供应商：
            "partEnvProtectionCategory":"",//环保类别:
            "partApplicationReason":"",//申请原因
            "partNote":""//备注说明：
        };
        var datalist=[];
        var noneArr=[];
        for(var i=0;i<trLen.length;i++){
            var dataArr=[];
            for(var m=0;m<tdLen.length;m++){
                var input = $(trLen).eq(i).find("td").eq(m).find("input").val();
                if($(trLen).eq(i).find("td").eq(m).find("input").length==0&&$(trLen).eq(i).find("td").eq(m).find("select").length==0){
                    input=$(trLen).eq(i).find("td").eq(m).attr("data-submitdata");
                }else{
                    if(input!=undefined){
                        if(input=="<自动生成>"){
                            input="";
                        }
                        if(m!=0&&m!=1&&m!=10&&m!=11&&m!=12&&(input=="")&&m<(commonObj.CEditAttName.length+2)){
                            $(trLen).eq(i).find("td").eq(m).css({
                                "background":"rgb(255,255,170)",
                                "-webkit-transition": "all .5s ease-in",
                                "-moz-transition": "all .5s ease-in",
                                "transition": "all .5s ease-in"

                            });
                            noneArr.push(m);
                        }else if(m!=0&&m!=1&&m!=10&&m!=11&&m!=12&&(input!="")&&m<(commonObj.CEditAttName.length+2)){
                            $(trLen).eq(i).find("td").eq(m).css({
                                "background":"#fff",
                                "-webkit-transition": "all .5s ease-in",
                                "-moz-transition": "all .5s ease-in",
                                "transition": "all .5s ease-in"

                            });
                        }
                    }else{
                        input=$(trLen).eq(i).find("td").eq(m).find("select option:selected").text();
                        if(m!=0&&m!=1&&m!=10&&m!=11&&m!=12&&(input=="")&&m<(commonObj.CEditAttName.length+2)){
                            $(trLen).eq(i).find("td").eq(m).css({
                                "background":"rgb(255,255,170)",
                                "-webkit-transition": "all .5s ease-in",
                                "-moz-transition": "all .5s ease-in",
                                "transition": "all .5s ease-in"

                            });
                            $(trLen).eq(i).find("td").eq(m).find(".layui-form [lay-ignore] ").css({
                                "background":"rgb(255,255,170)",
                                "-webkit-transition": "all .5s ease-in",
                                "-moz-transition": "all .5s ease-in",
                                "transition": "all .5s ease-in"
                            })
                            noneArr.push(m);
                        }else if(m!=0&&m!=1&&m!=10&&m!=11&&m!=12&&(input!="")&&m<(commonObj.CEditAttName.length+2)){
                            $(trLen).eq(i).find("td").eq(m).css({
                                "background":"#fff",
                                "-webkit-transition": "all .5s ease-in",
                                "-moz-transition": "all .5s ease-in",
                                "transition": "all .5s ease-in"

                            });
                            $(trLen).eq(i).find("td").eq(m).find(".layui-form [lay-ignore] ").css({
                                "background":"#fff",
                                "-webkit-transition": "all .5s ease-in",
                                "-moz-transition": "all .5s ease-in",
                                "transition": "all .5s ease-in"

                            })
                        }
                    }
                }

                dataArr.push(input);
            }

            var dataV=[];
            for(var v in dataListObj){
                dataV.push(v)
            }
            var obj={"roottype":"SemiFinishedGood"};
            for(var n=0;n<dataV.length;n++){
                obj[dataV[n]]=dataArr[n];

            }
            obj["roottype"]="SemiFinishedGood";
            datalist.push(obj);
        }
        if(noneArr.length>0){
            layerFn("请将带*号的属性填写完整");
            return;
        }
        var url="/MaterialsApplication/tmCreateMaterialsApplication";
        var data={
            datalist:datalist
        }
        $("#loading").css({
            display:"block"
        });
        ajaxNewFn(url,data,function(str){
            $("#loading").css({
                display:"none"
            });
            if(str.numberlist!=undefined&&str.numberlist.length>0){
                var num="";

                for(var k=0;k<str.numberlist.length;k++){
                    if(k!=(str.numberlist.length-1)){
                        num+=str.numberlist[k]+'；'
                    }else{
                        num+=str.numberlist[k]
                    }
                }

                var mesg="编号为："+num+"的物料，在配套物料申请编号"+str.econumber+"中创建成功"
                layerFn(mesg,function(){
                    window.location.href="about:blank";
                    window.opener=null;
                    window.open('','_self');
                    window.close();
                })
            }else{
                var mesg="物料在配套物料申请编号"+str.econumber+"创建失败"
                layerFn(mesg,function(){
                    window.location.href="about:blank";
                    window.opener=null;
                    window.open('','_self');
                    window.close();
                })
            }
        })

    })

}